#!/bin/bash    
#########################################################################
#
# Shell script for running DALTON test suite
#
# First version by Christof Haettig and Kasper Hald, Sep. 2000
#
#########################################################################

# Note: DALTON points to the default installation directory,
#       in the 'configure' script. If the person installing
#       Dalton has chosen another installation directory when
#       he/she ran 'configure', the you should modify the
#       DALTON definition to pint to that place.

if [ -x '/usr/bin/time' ]; then
 TIMING_OF="/usr/bin/time" #hjaaj sep07: gnu time gives more info than built-in time
else
 TIMING_OF="time"
fi
DALTON="`pwd`/../../bin/dalton"

#########################################################################
# define all the different test suites
#########################################################################
ESSENTIAL=""
#
SHORTENRGY="energy_direct energy_direct_eri energy_douglaskroll energy_nosymm energy_restart \
energy_symm energy_zmat energy_nevpt2_gs energy_nevpt2_ex energy_hsrohf energy_hsrohf_qc"
SHORTDFT="dft_hcth120 dft_camb3lyp dft_pbe"
SHORTGOPT="geoopt_cartsad geoopt_cartsad2 geoopt_constrain1a \
geoopt_constrain1b geoopt_delintsad geoopt_exci2 geoopt_numgrd \
geoopt_preopt geoopt_prop geoopt_prop2 geoopt_redintmin geoopt_vrml"
SHORTPROP="prop_ecd prop_or prop_exci prop_nolondon prop_spinspin prop_spinspin2 \
prop_spinspin3 prop_spinspin4 prop_socvir prop_vibvcd prop_ctocd prop_soppactocd \
prop_newtrasoppa prop_newtrasoppacc prop_newtramcscf aba_prop_file \
prop_vibg1 prop_vibg2"
SHORTRSP="rsp_2ndharm rsp_3rdharm \
rsp_3rdmom rsp_dresqr rsp_ecp rsp_exci rsp_excipolar2 rsp_hyperpolar \
rsp_lrso rsp_lrso2 rsp_mcdbterm rsp_mnf rsp_phosph rsp_polar rsp_propave \
rsp_socorr rsp_socorr2 rsp_socorr3 rsp_socorr_dir rsp_socorr2_dir \
rsp_socorr3_dir rsp_solvhyper rsp_soppa1excinosymm rsp_soppa1excisymm \
rsp_soppa3excinosymm rsp_soppa3excisymm rsp_soppafcnosymm rsp_soppafcsymm \
rsp_soppapolar rsp_soppapolarnosymm rsp_soppapolarsymm rsp_sreslrso \
rsp_sresqr rsp_ctocdhyper prop_spinspin5 prop_nolondon_soppacc rsp_soppaccexci \
rsp_soppaccpolar rsp_g_rohf rsp_g_rohf_dir rsp_g_cas \
rsp_g_rohf_ecc rsp_hfc rsp_zfs rsp_zfs_mc rsp_absorp rsp_prop_file"
SHORTWALK="walk_gradex walk_image walk_modeflw walk_polar walk_vibave walk_vibave2"
SHORTREST="r12_sym dpt_h2s dft_qr_sym"
AOSOPPA="prop_exci_ao prop_exci_aorpa prop_exci_aorpad prop_exci_aosoppa \
prop_exci_aosoppcc"
SHORTNOCC="$SHORTENRGY $SHORTDFT $SHORTGOPT $SHORTPROP $SHORTRSP $SHORTWALK $SHORTREST $AOSOPPA"
SHORTQM3="hfmm_1 cc2mm_spc_fop \
dftmm_1 dftmm_2 dftmm_3 dftmm_4 dftmm_5 dftmm_6 dftmm_7 dftmm_8 dftmm_9 \
qmmm1 qmmm2 qmmm3 qmmm4 qmmm5 qmmm6 qmmm7 qmmm8"
#
SHORTCC="cc_rsp_alpha2 cc_rsp_beta cc_rsp_beta2 cc_rsp_betafreq \
cc_rsp_betafreq2 cc_fop_drw2e cc_fop_prop cc_fop_prop2 cc_exgr_prop \
cc_rsp_excipolar cc_rsp_exci cc_energy_field cc_fop_prop3 cc_fop_prop4 \
cc_fop_freeze cc_rsp_gamma cc_rsp_gammafreq cc_rsp_gammafreq2 \
cc_rsp_mcdbterm cc_rsp_threephot cc_energy_triples cc_energy_triplesdirect \
cc_energy_triplesdirect2 cc_energy_triplesdirect3 cc_energy_triplet2 \
cc_rsp_twophot cc_rsp_twophot2 cc_rsp_gamma3 \
cc_rsp_twophot5 cc_rsp_twophotb cc_grad cc_geoopt_mp2 cc_fop_cc3 cc_rsp_ctocd \
cc_grad_ccpt_hf cc_geopt_ccpt_hf cc_geopt_ccpt_h2o cc_prop_file"
#
SHORT="$SHORTNOCC $SHORTCC $SHORTQM3"
#
MEDIUM="energy_corehole energy_solv geoopt_cartmin geoopt_dckerr \
geoopt_delintmin geoopt_exci geoopt_mp2froz geoopt_redintsad \
geoopt_symbrk prop_expgrad prop_nucquad prop_roa rsp_dckerr rsp_esr \
rsp_esr2 rsp_mnfphosph rsp_solvesr walk_polar2"
#
MEDIUMCC="cc_rsp_alpha cc_rsp_gamma2 cc_rsp_gammadirect cc_rsp_oddisp \
cc_energy_triplet cc_rsp_twophotdirect cc_grad2 cc_grad3 cc_geoopt \
cc_rsp_solv cc_energy_triplet3 cc2dc_energy cc2dc_resp cc2dc_exci cc2dc_resp_newlr \
cc_beta_solv" 
#
MEDIUMQM3="ccsdmm_spc_fop ccsdmm_oldtg ccsdmm_ec1_fop \
ccsdmm_ec3_fop ccsdmm_e01_fop ccsdmm_modelmix cc2mm_exci \
cc2mm_atmvdw ccsdmm_twopar_vdw cc2mm_lr_qr cc2mm_spc_exci ccmm_beta"
#
MEDIUM="$MEDIUM $MEDIUMCC $MEDIUMQM3"
#
LONG="geoopt_prop3 prop_vibana rsp_excipolar rsp_twophot walk_solvmag"
#
LONGCC="cc_geoopt2"
#
LONG="$LONG $LONGCC"
#
PARALLEL="energy_parallel geoopt_parallel prop_parallel rsp_parallel dft_parallell_properties"
#
DFT="dft_b3lyp_cart dft_b3lyp_nosym dft_b3lyp_sym dft_blyp_nosym \
dft_blyp_sym dft_energy_sym dft_lda_cart dft_lda_nosym dft_lda_sym \
dft_optimize_nosym dft_optimize_sym dft_lb94 dft_qr_sym dft_qr_nosym \
dft_lda_molhes_nosym dft_b3lyp_molhes_nosym dft_b3lyp_magsus_nosym \
dft_polar dft_cr_sym"
#
DPT="dpt_hf dpt_h2s dpt_h2s_finp dpt_hbr_finm dpt_h2s_finm \
dpt_hbr dpt_hbr_finp dpt_h2s_ff"
#
MP2R12="r12_damped r12_damped_herdir \
r12_sym r12_nosym r12_fc_sym r12_fc_nosym \
r12_aux_sym r12_aux_nosym r12_fc_aux_sym r12_fc_aux_nosym \
r12_prop r12_prop_n2 r12_prop_h2o r12_loc r12_stg r12_orb r12_cabs"
#
SHORTCCR12="cc2_r12_aux2_bh cc2_r12_aux2_bh_sym \
cc2_r12_aux_bh_a2 cc2_r12_aux_bh_sym_a2 \
cc2_r12_he cc2_r12_aux2_bh_a2 cc2_r12_aux2_bh_sym_a2 \
cc2_r12_aux2_bh_lhtr cc2_r12_aux2_bh_sym_lhtr \
cc2_r12_he_a2 ccsdr12_bh_direct ccsdr12_n2_direct \
ccsdr12_bh ccsdr12_n2 cc2_r12_aux_ch4 cc2_r12_aux_ch4_sym \
cc2_r12_aux_ch4_lhtr cc2_r12_aux_ch4_sym_lhtr cc2_r12_aux_nh3_sym \
cc2_r12_aux_nh3_sym_lhtr ccsdr12pt_ne \
cc2_r12_aux_ch4_fop cc2_r12_aux_ch4_sym_fop \
cc2_r12_aux_h2o_fop cc2_r12_aux_h2o_sym_fop \
cc2_r12_aux_ch4_lr cc2_r12_aux_ch4_sym_lr \
cc2_r12_aux_ch4_sym_lr_nondir cc2_r12_aux_ch4_sym_nondir \
cc2_r12_aux_ch4_ff cc2_r12_aux_ch4_ff2 \
cc2_r12_aux_ch4_sym_pol cc2_r12_aux_n2_gamma cc2_r12_aux_n2_gamma2 \
cc2_r12_aux_ne_gamma cc2_r12_aux_bh_a3 cc2_r12_aux_bh_sym_a3 \
cc2_r12_aux_ne cc2_r12_aux_ne_gamma2 cc2_r12_aux_h2o_a2 \
cc2_r12_aux_h2o_sym_a2 cc2_r12_aux_h2o_a3 cc2_r12_aux_h2o_sym_a3 \
ccsdr12_n2_exc ccsdr12_n2_sym_exc ccsdr12_n2_sym_exc_lhtr \
ccsdr12pt_ne_f12 cc2_r12_aux_ch4_sym_f12 cc2_r12_aux_ch4_sym_a3_f12 \
ccsdr12_n2_gamma ccsdr12_n2_gamma2 ccsdr12_h2o_gamma ccsdr12_h2o_gamma2 \
ccsdr12_be cc2_r12_aux_ch4_cbs1 cc2_r12_aux_ch4_cbs2 cc2_r12_aux_ch4_cbs3"
#
LONGCCR12="cc2_r12_aux_ch4_a2 cc2_r12_aux_ch4_sym_a2 \
cc2_r12_aux_ch4_sym_a3 cc2_r12_aux_benzene_sym_fop cc3r12_n2_sym_exc \
cc3r12_n2_sym_exc_lhtr cc2_r12_aux_ch4_cbs2a"
#
SRDFT="hfsrlda_sym mp2srlda_ener_nosym mcsrlda_sym mcsrpbe_sym \
hfsrpbe_sym mp2srpbe_ener_nosym"
#SRDFT="mp2srlda_ener_nosym"
#
#
CCR12="$SHORTCCR12 $LONGCCR12"
#
R12="$MP2R12 $SHORTCCR12 $LONGCCR12"
#
QM3="$SHORTQM3 $MEDIUMQM3"
#
NUMDER="hfreqfromE hfreqfromg effgeom_fromE effgeom_fromE2 vibave_spinspin"
#
GEOOPT="cc_geoopt cc_geoopt2 cc_geoopt_mp2 geoopt_cartmin geoopt_cartsad \
geoopt_cartsad2 geoopt_constrain1a geoopt_constrain1b geoopt_dckerr \
geoopt_delintmin geoopt_delintsad geoopt_exci geoopt_exci2 geoopt_mp2froz \
geoopt_numgrd geoopt_preopt geoopt_prop geoopt_prop2 geoopt_prop3 \
geoopt_redintmin geoopt_redintsad geoopt_symbrk geoopt_vrml"
#
ALL="$SHORT $MEDIUM $LONG $DFT $NUMDER $R12 $DPT"
#
ALLCC="$SHORTCC $MEDIUMCC $LONGCC"
#
BENCHMARK="benchmark_eri_r12 benchmark_eri_r12xl \
benchmark_eri_adzs benchmark_her_adzs benchmark_eri_adz benchmark_her_adz"
run_new=0;
#
#########################################################################
# function usage(): print usage information 
#########################################################################
usage() {
 cat <<'%EOF%'
usage: TEST [-h|-help|--help] [-keep] [-reftest] [-bell] [-benchmark]
            [-param "option list"] [-dalton script] [-log logfile] testcase

       -h | -help | --help  : show this help description
       -keep                : keep *.mol, *.dal, *.check and *.log files
       -reftest             : test reference file(s); do not run any
                              calculations
       -bell                : sound bell each time a test fails
       -benchmark           : print CPU time for each testcase as part of
                              the summary
       -param "option list" : pass "option list" to dalton-script
       -dalton script       : use script instead of the default
                              dalton-script "`pwd`/../dalton" (absolute path) 
       -log logfile         : write output into logfile instead of the
                              default "TESTLOG"

       and where testcase might be one of the following:
          essential            -- run a minimal set of essential testcases
          parallel             -- run all parallel testcases
          allcc                -- run all coupled-cluster testcases
          short                -- run all short testcases
          medium               -- run all medium length testcases
          long                 -- run all long testcases
          dft                  -- run all DFT testcases
          srdft                -- run all srDFT testcases
          r12                  -- run all R12 testcases
          ccr12                -- run all CC-R12 testcases
          mp2r12               -- run all MP2-R12 testcases
          shortccr12           -- run all short CC-R12 testcases
          longccr12            -- run all long CC-R12 testcases
          benchmark            -- run all BENCHMARK testcases
          dpt                  -- run all DPT testcases
          numder               -- run all numerical derivatives testcases
          geoopt               -- run all OPTIMIZE testcases
	  aosoppa              -- run all AOSOPPA testcases
          pcm                  -- run all pcm testcases
          perl                 -- run all perl-style testcases 
          all                  -- run all non-parallel testcases
                                  (= short + medium + long)
          <case1 [case2] ...>  -- run only the specified testcase(s)
%EOF%
exit 1
}

#########################################################################
# function myprint(string): print to stdout and $listing
#########################################################################
myprint(){
 echo "$1";
 echo "$1" >> $listing;
}

#########################################################################
#########################################################################
# start with real work:
#########################################################################
#########################################################################

#########################################################################
# set defaults and evaluate parameters
#########################################################################
TESTparams="$0 $*"
help=""
keep="false"
paramlist=""
reftest=""
bell=""
benchmark=""
listing="TESTLOG"
perl_log="test.log"
perl_err="test.err"
emptystring="                                "
answer_yes=
keep_quiet=

tmpfile=/tmp/dalton-$$.log
touch "$tmpfile"   || { echo "cannot create temporary file. Aborting execution."; exit 1; }
test -f "$tmpfile" || { echo "Possible security breach related to temporary file."; exit 1; }

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

while echo $1 | egrep -q '^-'; do
   case $1 in
     "-h" | "-help" | "--help" ) help="true" ; break;;
     "-dalton" )  shift; DALTON=$1; shift;;
     "-log" ) shift; listing=$1; shift;;
     "-keep" ) keep="true"; shift;;
     -param* ) shift; paramlist="$1"; shift;;
     "-reftest" ) reftest="true"; shift;;
     "-bell" ) bell="true"; shift;;
     "-y") answer_yes=true; shift;;
     "-q") keep_quiet=true; shift;;
     "-benchmark" ) benchmark="true"; shift;;
     * ) echo "---> ERROR: $1 is an unknown option!"; usage;;
   esac
done
if [ $help ]; then
  usage
fi

if [ ! -x $DALTON ]; then
   echo ' '
   echo '----> ERROR, the "dalton" job script '
   echo    '"'$DALTON'"'
   echo '  does not exist or is not executable.'
   echo 'The person who installed dalton has probably selected'
   echo '  another place for the "dalton" job script.'
   echo 'Please modify the DALTON variable in this script (i.e. '$0')'
   echo '  to point to that place.    Alternatively, you may'
   echo '  specify where the dalton script is with the -dalton option'
   echo ' '
#  usage
   exit 1
fi

#########################################################################
# set list of test cases:
#########################################################################
testcase=$*
if   [ "$testcase" = "" ]; then
  echo '    ***********************************'
  echo '    * ERROR: No testcase(s) specified *'
  echo '    ***********************************'
  usage
fi
testcase=""
perl_style_kwd=""
perl_style=0
all_perl_style=0
while [ -n "$1" ]; do
   case $1 in
  "essential" )
   testcase="$testcase $ESSENTIAL"; 
   perl_style_kwd="$perl_style_kwd $1";
   shift ;;
  "energy" )
   testcase="$testcase $SHORTENRGY"; 
   perl_style_kwd="$perl_style_kwd $1";
   shift ;;
  "pcm" )
   perl_style_kwd="$perl_style_kwd $1";
   shift ;;
  "short_nocc" )
   testcase="$testcase $SHORTNOCC"; 
   shift ;;
   "shortcc" )
   testcase="$testcase $SHORTCC"; 
   shift ;;
  "short" )
   perl_style_kwd="$perl_style_kwd $1";
   testcase="$testcase $SHORT";
   shift ;;
  "medium" )
   perl_style_kwd="$perl_style_kwd $1";
   testcase="$testcase $MEDIUM"; 
   shift ;;
  "long" )
   perl_style_kwd="$perl_style_kwd $1";
   testcase="$testcase $LONG"; 
   shift ;;
  "parallel" )
   if [ "$paramlist" = "" ]; then
     paramlist="-N 3"
   fi
   testcase="$testcase $PARALLEL"; 
   shift ;;
  "all" )
   all_perl_style=1;
   testcase="$testcase $ALL"; 
   shift ;;
  "perl" )
   echo "perl"
   all_perl_style=1;
   shift ;;
  "allcc" )
   testcase="$testcase $ALLCC"; 
   shift ;;
  "dft" )
   perl_style_kwd="$perl_style_kwd $1";
   testcase="$testcase $SHORTDFT $DFT"; 
   shift ;;
  "srdft" )
   perl_style_kwd="$perl_style_kwd $1";
   testcase="$testcase $SRDFT"; 
   shift ;;
  "dpt" )
   testcase="$testcase $DPT"; 
   shift ;;
   "r12" )
   testcase="$testcase $R12"; 
   shift ;;
   "ccr12" )
   testcase="$testcase $CCR12"; 
   shift ;;
   "mp2r12" )
   testcase="$testcase $MP2R12"; 
   shift ;;
   "shortccr12" )
   testcase="$testcase $SHORTCCR12"; 
   shift ;;
   "longccr12" )
   testcase="$testcase $LONGCCR12"; 
   shift ;;
   "benchmark" )
   testcase="$testcase $BENCHMARK"; 
   shift ;;
   "numder" )
   testcase="$testcase $NUMDER"; 
   shift ;;
   "geoopt" )
   testcase="$testcase $GEOOPT"; 
   shift ;;
   "aosoppa" )
   testcase="$testcase $AOSOPPA"; 
   shift ;;
   "qm3" )
   testcase="$testcase $QM3"; 
   shift ;;
   "*parallel*" )  # assume parallel test if "parallel" in name
   if [ "$paramlist" = "" ]; then
     paramlist="-N 3"
   fi
   testcase="$testcase $1"; 
   shift ;;
   * )
   testcase="$testcase $1"; 
   shift ;;
   esac
done

if [ -n "$perl_style_kwd" -o  "$all_perl_style" -eq 1 ]; then
    perl_style=1
fi

#########################################################################
# create a unique directory for test output /hjaaj
# nonportable characters in the directory name removed. /pawsa
#########################################################################
testdir="`date +'%Y-%m-%dT%H_%M'`-testjob-pid-$$"
while [ -e $testdir ]; do
    testdir=${testdir}x
done
mkdir $testdir 
cp functions $testdir
cd $testdir || exit 1


#########################################################################
# check files for test listing:
#########################################################################
if [ -s $listing -a "$answer_yes" = '' ]; then
   echo "$listing already exists... should it be deleted first? (y/n)"
   read answer
   if [ "$answer" = "yes"  -o  "$answer" = "y" -o "$answer" = "Y" ]; then
       rm -f $listing
   fi
fi
if [ -s $perl_log  -a "$answer_yes" = '' ]; then
   echo "$perl_log already exists... should it be deleted first? (y/n)"
   read answer
   if [ "$answer" = "yes"  -o  "$answer" = "y" -o "$answer" = "Y" ]; then
       rm -f $perl_log
   fi
fi
if [ -s $perl_err  -a "$answer_yes" = '' ]; then
   echo "$perl_err already exists... should it be deleted first? (y/n)"
   read answer
   if [ "$answer" = "yes"  -o  "$answer" = "y" -o "$answer" = "Y" ]; then
       rm -f $perl_err
   fi
fi

myprint "#####################################################################"
myprint "                          DALTON test suite"
myprint "#####################################################################"
myprint "invoked with          : $TESTparams"
myprint "date and time         : `date`"
myprint "dalton script         : $TIMING_OF $DALTON"
myprint "parameter list passed : $paramlist"
myprint "test job directory    : $testdir"
myprint "test listing          : $listing"
myprint "test cases            : $testcase"

#########################################################################
# loop over test cases:
#########################################################################
passedall="ALL TESTS ENDED PROPERLY!"
problems=""
numresult=0
for item in ${testcase}
do
  trouble=0
  myprint "###########################################################"
  myprint "`date`"
  myprint "Starting now with test $item:"
  myprint "-----------------------------------------------------------"
  chmod +x ../$item || exit 1
  ../$item
# remove any directory path now the files are unpacked
  item=`basename $item`
  if [ -f $item.info -a "$keep_quiet" = "" ]; then
    cat $item.info
    cat $item.info >> $listing
  fi
# Check if this is a multistep job
  numsteps=1
  if [ -r ./$item'__'1.dal ]; then
      multi=1
      numsteps=`ls ./$item\_\_[0-9].dal | wc -l`
      myprint "   This is a multi-step job consisting of $numsteps steps"
      rm -f ./$item'__'[0-9]'_'$item.log
      chmod +x ./$item'__'[0-9].check
  else
      multi=0
      rm -f ./$item.log
      chmod +x ./$item.check
  fi
# Multi-step tests loop over the different jobsteps, regular tests just
# go through this once
  step=1
  while [ "$step" -le "$numsteps" ]
  do
      potfile=
      if [ "$multi" -eq 1 ]; then
          myprint "-----------------------------------------------------------"
          myprint "  Start $item step $step of $numsteps :"
          myprint "-----------------------------------------------------------"
          molfile=$item
          if [ -r $item.pot ]; then potfile=$item ; fi
	  cp $item'__'$step'.dal' $item'.dal'
          dalfile=$item
          logfile=./$item'__'$step'_'$item.log
          checkfile=./$item'__'$step.check
          reffile=./$item'__'$step.ref
          moldenfile=./$item'__'$step'_'$item.molden
      else
          molfile=$item
          if [ -r $item.pot ]; then potfile=$item ; fi
          dalfile=$item
          logfile=./$item.log
          checkfile=./$item.check
          reffile=./$item.ref
          moldenfile=./$item.molden
      fi
# If it's a reftest, no calculation is performed
      if [ "$reftest" = "true" ]; then
          myprint ""
          myprint "evaluate reference output file $reffile:"
          myprint "-----------------------------------------------------------"
          if [ ! -r $reffile ]; then
	      cp ../trueresult/$reffile .
          fi
          checkout=`$checkfile $reffile | tee -a $listing`
          if [ "$benchmark" = "true" ]; then
              if [ `$GREP "CPU  time used in DALTON" $reffile | wc -l` = 1 ]; then
                  CPU_usage=`$GREP "CPU  time used in DALTON" $reffile | sed s/">>>> Total CPU  time used in DALTON\:"/""/`
              else
                  CPU_usage="N/A"
              fi
          fi
          if [ "$checkout" = "TEST ENDED AS EXPECTED" ]; then
              checkout="TEST ENDED PROPERLY"
          fi
      else
          failed=false
          if [ "$step" -eq "1" ]; then
#             make sure that directory is clear before start
              $TIMING_OF $DALTON -d $paramlist -ext log $dalfile $molfile $potfile > "$tmpfile"
              if [ $? -ne 0 ]; then failed=true; fi
          else
#             copy $item.tar.gz for multistep jobs (-f option)
              $TIMING_OF $DALTON -f $item $paramlist -ext log $dalfile $molfile $potfile > "$tmpfile"
              if [ $? -ne 0 ]; then failed=true; fi
          fi
          grep -v '\*\*\*' $tmpfile | grep -v -i 'WARNING' | grep -v -i 'INFO'| \
          grep -v 'OUTPUT FROM' | grep -v 'Version' | grep -v 'PID' | \
          grep -v '^$' | tee -a $listing

          if [ "$multi" -eq 1 ]; then
	      cp $molfile'.log' $logfile
          fi
          myprint ""
          myprint "evaluate output file $logfile:"
          myprint "-----------------------------------------------------------"

          checkout=`$checkfile $logfile | tee -a $listing`

          # Successful tests report "TEST ENDED PROPERLY" unless
          # the test is meant to return non-zero exit code.  These
          # return "TEST ENDED AS EXPECTED".  In the latter case, we
          # change it to "TEST ENDED PROPERLY" if the exit code was
          # nonzero, otherwise we return "DALTON SCRIPT ERROR"

          if [ "$checkout" = "TEST ENDED PROPERLY" ]; then
              [ "$failed" = "true" ] && checkout="TEST FAILED ABNORMALLY"
          elif [ "$checkout" = "TEST ENDED AS EXPECTED" ]; then
              echo "checkout=$checkout, failed=$failed"
              if [ "$failed" = "true" ]; then
                  checkout="TEST ENDED PROPERLY"
              else
                  checkout="DALTON SCRIPT ERROR"                  
              fi
          fi
          if [ "$benchmark" = "true" ]; then
              if [ `$GREP "CPU  time used in DALTON" $logfile | wc -l` = 1 ]; then
                  CPU_usage=`$GREP "CPU  time used in DALTON" $logfile | sed s/">>>> Total CPU  time used in DALTON\:"/""/`
              else
                  CPU_usage="N/A"
              fi
          fi
      fi
      if [ "$benchmark" = "true" ]; then
          numresult=`expr $numresult \+ 1`
          if [ $step -eq 1 ]; then
              testlist[$numresult]="$item"
          else
              testlist[$numresult]="...step$step"
          fi
          timelist[$numresult]="$CPU_usage"
      fi
      echo $checkout
      passed=`echo $checkout | grep "TEST ENDED PROPERLY"`
      if [ -z "$passed" ]; then
        trouble=`expr $trouble \+ 1`
        if [ "$bell" = "true" ]; then
          echo "\a"
        fi
        passedall="THERE IS A PROBLEM IN TEST CASE(S)"
        if [ "$trouble" -eq 1 ]; then
            problems="$problems $item"
        fi
      elif [ "$keep" = "false" ]; then
          rm -f $dalfile.dal $dalfile.BAS $checkfile $logfile $item.info
          if [ "$step" -eq "$numsteps" ]; then
#         only delete restart files in last step of multistep test !!!
             rm -f $dalfile.tar.gz
          fi
#     else everything OK and -keep defined
      fi
    step=`expr $step \+ 1`
  done
  if [ "$trouble" -eq 0 ]; then
      if [ "$keep" = "false" ]; then
          rm -f $molfile.mol
          rm -f $potfile.pot
      fi
  fi
done

#########################################################################
# PCM test suite
#########################################################################
if [ "$perl_style" -gt 0 ]; then
    perl_log="perl_tests.log"
    perl_err="perl_tests.err"
    perl_fail=""
    myprint ""
    myprint "Now running perl-style tests..."
    myprint ""
    cp ../test.pl .    
    cp ../*.tst .
    cp ../checklist .
    options=""
    parameters="--dalton=$DALTON --log=$perl_log --err=$perl_err"
    if [ "$keep" = "true" ]; then
	parameters="$parameters --keep"
    fi
    if [ "$reftest" = "true" ]; then
	parameters="$parameters --checkref"
    fi
    if [ -n "$perl_style_kwd" ]; then
	parameters="$parameters --keyword $perl_style_kwd"
    else
	parameters="$parameters --all"
    fi
    if [ "$paramlist" != "" ]; then
	options="--options=$paramlist"
    fi
    perl_fail=""
    myprint "Perl-style testsuite invoked with the following parameters"
    myprint "$parameters $options"
    ./test.pl $parameters "$options"
    perl_exit=$?
    if [ $perl_exit -eq 0 ]; then
	myprint "Perl-style tests computed successfully"
    elif [ $perl_exit -eq 1 ]; then
        passedall="THERE IS A PROBLEM IN TEST CASE(S)"
        perl_fail="`grep -A1 'tests have failed' $perl_log | tail -1`"
	problems="$problems $perl_fail"
        myprint "THERE IS A PROBLEM in perl-style tests"
    else
        passedall="THERE IS A PROBLEM IN TEST CASE(S)"
	myprint ""
	myprint "WARNING WARNING: Perl-style tests died unexpectedly!"
    fi
    myprint ""
    myprint "Perl-style tests finished!"
    rm test.pl 
    rm *.tst
    rm checklist
    cat $perl_log >> $listing
fi

#########################################################################
# final result:
#########################################################################

myprint ""
myprint "#####################################################################"
myprint "                              Summary"
myprint "#####################################################################"
myprint ""

if [ "$benchmark" = "true" ]; then
    if [ "$numresult" -gt 0  ]; then
      ind=1
      while [ "$ind" -le "$numresult" ]; do
        awk "END{printf \"%32s %s\\n\", \"${testlist[$ind]}\",\"${timelist[$ind]}\"}"</dev/null 
        awk "END{printf \"%32s %s\\n\", \"${testlist[$ind]}\",\"${timelist[$ind]}\"}"</dev/null >> $listing
        ind=`expr $ind \+ 1`
      done
      echo
    fi
fi

myprint "$passedall"
myprint "$problems"
rm "$tmpfile"
myprint "date and time         : `date`"

exit 0
